﻿/**
 *----------------------------------------
 * @file XMLParser.cpp
 * Rodeo Game Engine source file
 * Copyright(c) 2010-2011 Marcin Goryca
 *----------------------------------------
 */
#include "tools\xmlparser.h"

using namespace rodeocore;
using namespace std;

namespace rodeotools
{
bool XmlParser::read(const char* filepath)
{
// Step 1. Try to load XML file
    
    try
    {
        if(!xml_document_.LoadFile(filepath))
        {
            throw -1;
        }
    }
    catch(...)
    {
        MessageBox(NULL, "Couldn't load XML File!", HEADER, errIconOK);
        return false;
    }

// Step 2. Assign root element of document to element object 
    xml_element_ = xml_document_.FirstChildElement();
    std::string el = xml_element_->Value();
    std::string el_attr = xml_element_->Attribute("name");

    

    if(xml_element_ == NULL)
    {
        xml_document_.Clear();
        MessageBox(NULL, "Failed to load XML file. No root element", HEADER, errIconOK);
        return false;
    }

// Step 3. Loop through whole document and retrieve information

    for(TiXmlElement* elem = xml_element_->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
    {
        std::string element = elem->Value();

        for(TiXmlElement* child_elem = elem->FirstChildElement(); child_elem != NULL; child_elem = child_elem->NextSiblingElement())
        {
            std::string element = child_elem->Value();
            if(child_elem->GetText() != NULL)
            {
                const char* value = child_elem->GetText();
                
                if(isdigit(value[0]))
                {
                    int value_int = 0;

                    value_int = rodeomath::toInt(value);
                    map_int_[element] = value_int;
                }
                else
                {
                    std::string value_string;

                    value_string = child_elem->GetText();

                    map_string_[element] = value_string;

                }
            }
            else
            {
                std::string value = "";
                int value_int = 0;
                map_int_[element] = value_int;
            }
        }
    }

    return true;
}
}